//
//  XDContactsDetailViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-30.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDContactsDetailViewController.h"
#import "DBFrameService.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "ContactsUtil.h"
#import "XDMailSendViewController.h"

@interface XDContactsDetailViewController ()
{
    NSMutableDictionary *userDic;
    NSString *userguid;
}
@end

@implementation XDContactsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    userguid = [[intentParams objectForKey:@"userguid"]retain];
    userDic = [[DBFrameService getUserDetailByUserguid:userguid]retain];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tv release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTv:nil];
    [super viewDidUnload];
}

#pragma tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 4;
            break;
        case 2:
            return 1;
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch ([indexPath indexAtPosition:0]) {
        case 0:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    return 128;
                    break;
            }
            break;
    }
    return 45;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return nil;
            break;
        case 1:
            return @"联系方式";
            break;
        case 2:
            return @"操作";
            break;
    }
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[[UITableViewCell alloc]init]autorelease];
    switch ([indexPath indexAtPosition:0]) {
        case 0:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    
                    [cell.contentView addSubview:[self getDetailImageView:[userDic objectForKey:@"sex"]]];
                    UILabel *lbName = [[UILabel alloc]initWithFrame:CGRectMake(128, 25, 170, 25)];
                    [lbName setText:[userDic objectForKey:@"displayname"]];
                    [lbName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:25]];
                    [lbName setBackgroundColor:[UIColor clearColor]];
                    lbName.textAlignment = UITextAlignmentCenter;
                    [cell.contentView addSubview:lbName];
                    [lbName release];
                    
                    UILabel *lbInfo = [[UILabel alloc]initWithFrame:CGRectMake(128, 55, 170, 20)];
                    [lbInfo setText:[userDic objectForKey:@"ouname"]];
                    [lbInfo setFont:[UIFont fontWithName:@"Helvetica" size:18]];
                    [lbInfo setBackgroundColor:[UIColor clearColor]];
                    lbInfo.textAlignment = UITextAlignmentCenter;
                    [cell.contentView addSubview:lbInfo];
                    [lbInfo release];
                    
                    UILabel *lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(128, 80, 170, 20)];
                    [lbTitle setText:[userDic objectForKey:@"title"]];
                    [lbTitle setFont:[UIFont fontWithName:@"Helvetica" size:18]];
                    [lbTitle setBackgroundColor:[UIColor clearColor]];
                    lbTitle.textAlignment = UITextAlignmentCenter;
                    [cell.contentView addSubview:lbTitle];
                    [lbTitle release];
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
            }
            break;
        case 1:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    [cell.contentView addSubview:[self getTag:@"手机号"]];
                    [cell.contentView addSubview:[self getAttribute:[userDic objectForKey:@"mobile"]]];
                    break;
                case 1:
                    [cell.contentView addSubview:[self getTag:@"办公电话"]];
                    [cell.contentView addSubview:[self getAttribute:[userDic objectForKey:@"teleoffice"]]];
                    break;
                case 2:
                    [cell.contentView addSubview:[self getTag:@"家庭电话"]];
                    [cell.contentView addSubview:[self getAttribute:[userDic objectForKey:@"telehome"]]];
                    break;
                case 3:
                    [cell.contentView addSubview:[self getTag:@"邮箱"]];
                    [cell.contentView addSubview:[self getAttribute:[userDic objectForKey:@"mail"]]];
                    break;
            }
            break;
        case 2:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    cell.textLabel.text = @"发送OA邮件";
                    cell.accessoryType = UITableViewCellSelectionStyleBlue;
                    break;
            }
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath indexAtPosition:0]) {
        case 1:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    [ContactsUtil call:@"手机号码" :[userDic objectForKey:@"mobile"]];
                    break;
                case 1:
                    [ContactsUtil call:@"办公电话" :[userDic objectForKey:@"teleoffice"]];
                    break;
                case 2:
                    [ContactsUtil call:@"家庭电话" :[userDic objectForKey:@"telehome"]];
                    break;
                case 3:
                    [ContactsUtil showAlertDetail:@"邮箱" :[userDic objectForKey:@"mail"]];
                    break;
            }
            break;
        case 2:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    [self sendOAMail];
                    break;
            }
            break;
    }
    
}



#pragma control

-(void)sendOAMail
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    
    [params setObject:[NSString stringWithFormat:@"%@;",userguid] forKey:@"guids"];
    [params setObject:[NSString stringWithFormat:@"%@;",[userDic objectForKey:@"displayname"]] forKey:@"names"];
    
    XDMailSendViewController *vc = [[XDMailSendViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

-(UILabel *)getTag:(NSString *)_tagName
{
    UILabel *lbMobilePhoneTag = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 45)];
    [lbMobilePhoneTag setText:_tagName];
    [lbMobilePhoneTag setFont:[UIFont fontWithName:@"Helvetica" size:15]];
    [lbMobilePhoneTag setBackgroundColor:[UIColor clearColor]];
    lbMobilePhoneTag.textAlignment = UITextAlignmentCenter;
    return [lbMobilePhoneTag autorelease];
}

-(UILabel *)getAttribute:(NSString *)_attribute
{
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 198, 45)];
    [lb setText:_attribute];
    [lb setFont:[UIFont fontWithName:@"Helvetica_bold" size:15]];
    [lb setBackgroundColor:[UIColor clearColor]];
    lb.textAlignment = UITextAlignmentCenter;
    return [lb autorelease];
}

-(UIImageView *)getDetailImageView:(NSString *)sexStr
{
    NSString *imgstr = @"img_man";
    if([sexStr isEqualToString:@"女"])
    {
        imgstr = @"img_woman";
    }
    
    UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgstr]];
    [iv setFrame:CGRectMake(10, 10, 108, 108)];
    
    iv.contentMode = UIViewContentModeScaleAspectFit;
    return [iv autorelease];
}

@end
